package com.ruoyi.init;

import com.ruoyi.flowable.common.constant.ProcessConstants;
import com.ruoyi.flowable.common.enums.ApproveWords;
import com.ruoyi.flowable.service.IFlowDefinitionService;
import com.ruoyi.flowable.service.ISysDeployFormService;
import com.ruoyi.system.domain.FlowProcDefDto;
import com.ruoyi.system.domain.SysDeployForm;
import com.ruoyi.system.domain.SysForm;
import com.ruoyi.system.mapper.FlowDeployMapper;
import com.ruoyi.system.mapper.SysFormMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
@Component
public class ApproveInit implements ApplicationRunner {
    @Autowired
    public IFlowDefinitionService flowDefinitionService;
    @Autowired
    @Resource
    private FlowDeployMapper flowDeployMapper;
    @Autowired
    private SysFormMapper sysFormMapper;
    @Autowired
    private ISysDeployFormService sysDeployFormService;

    /**
     * 启动时，检查审批流程是否部署，如没有部署则进行部署和挂载
     * 1、查看流程部署表，若流程已部署，则结束；
     * 2、读取流程XML文件，进行部署；
     * 3、查询流程部署ID、查询审批表单ID，进行挂载
     * @param args
     */
    @Autowired
    @Override
    public void run(ApplicationArguments args) {
        // 1、查看流程部署表，若流程已部署，则结束
        List<FlowProcDefDto> flowList = null;
        flowList = flowDeployMapper.selectDeployList(ApproveWords.FLOWNAME.getWord());
        if(flowList!=null && !flowList.isEmpty()){
            log.info("审批流程已部署！");
            return;
        }
        // 2、读取流程XML文件，进行部署
        log.info("审批流程部署开始！");
        InputStream in = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource(ProcessConstants.APPROVEXML);
            InputStream inputStream = classPathResource.getInputStream();
            String xml = IOUtils.toString(inputStream);
            in = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
            flowDefinitionService.importFile(ApproveWords.FLOWNAME.getWord(), "", in);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        // 3、查询流程部署ID、查询审批表单ID，进行挂载
        flowList = flowDeployMapper.selectDeployList(ApproveWords.FLOWNAME.getWord());
        String deployId = flowList.get(0).getDeploymentId();
        List<SysForm> sysForms = sysFormMapper.selectSysFormList(new SysForm());
        Long formId = 0L;
        for(SysForm form : sysForms){
            if (ApproveWords.FORMNAME.getWord().equals(form.getFormName())){
                formId = form.getFormId();
                break;
            }
        }
        SysDeployForm sysDeployForm = new SysDeployForm();
        sysDeployForm.setDeployId(deployId);
        sysDeployForm.setFormId(formId);
        sysDeployFormService.insertSysDeployForm(sysDeployForm);
    }
}
